import Vue from "vue";
import VueRouter from "vue-router";

// import Login from "../components/login.vue";
// import Home from "../components/home.vue";
// import Welcome from "../components/subcomponents/welcome.vue";
const Login = () => import(/* webpackChunkName: "Login_Home_welcome" */ '../components/login.vue')
const Home = () => import(/* webpackChunkName: "Login_Home_welcome" */ '../components/home.vue')
const Welcome = () => import(/* webpackChunkName: "Login_Home_welcome" */ '../components/subcomponents/welcome.vue')

// import Users from "../components/subcomponents/users.vue"
// import Powers from "../components/subcomponents/power.vue"
// import Roles from "../components/subcomponents/roles.vue"
const Users = () => import(/* webpackChunkName: "Users_Powers_Roles" */ '../components/subcomponents/users.vue')
const Powers = () => import(/* webpackChunkName: "Users_Powers_Roles" */ '../components/subcomponents/power.vue')
const Roles = () => import(/* webpackChunkName: "Users_Powers_Roles" */ '../components/subcomponents/roles.vue')


// import Categories from "../components/subcomponents/cate.vue"
// import Params from "../components/subcomponents/params.vue"
const Categories = () => import(/* webpackChunkName: "Categories_Params" */ '../components/subcomponents/cate.vue')
const Params = () => import(/* webpackChunkName: "Categories_Params" */ '../components/subcomponents/params.vue')

// import GoodsList from '../components/subcomponents/list.vue'
// import Add from "../components/subcomponents/add.vue"
const GoodsList = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/subcomponents/list.vue')
const Add = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/subcomponents/add.vue')

// import Order from "../components/subcomponents/order.vue"
// import Report from "../components/subcomponents/report.vue"
const Order = () => import(/* webpackChunkName: "Order_Report" */ '../components/subcomponents/order.vue')
const Report = () => import(/* webpackChunkName: "Order_Report" */ '../components/subcomponents/report.vue')

Vue.use(VueRouter);

const routes = [{
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/home",
    component: Home,
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      component: Welcome
    }, {
      path: '/users',
      component: Users
    },{
      path: '/rights',
      component: Powers
    },{
      path: '/roles',
      component: Roles
    },{
      path: '/categories',
      component: Categories
    },{
      path: '/params',
      component: Params
    },{
      path: '/goods',
      component: GoodsList
    },{
      path: '/goods/add',
      component: Add
    },{
      path: '/orders',
      component: Order
    },{
      path: '/reports',
      component: Report
    }]
  }
];

const router = new VueRouter({
  routes
});

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // form 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next() 放行     next("/login")强制跳转

  if (to.path === "/login") {
    return next();
  }
  const tokenStr = window.sessionStorage.getItem("token");
  if (!tokenStr) {
    return next("/login");
  }
  next();
});

export default router;